# -*- coding: UTF-8 -*-

import datetime
import sys
import time
from sqlalchemy import and_,  func, or_
from typing import Dict, List, Any, Generator

from sqlalchemy import Boolean, Column, DateTime, Integer, String, Index, Table, ForeignKey, text, JSON, TEXT, distinct, FLOAT
from sqlalchemy.orm import Session, relationship
from common.database import Base
from common.database import SessionMixin
from common.utils import is_empty
from core.logger import logger


__all__ = ['AuditEvent']

class AuditEvent(Base, SessionMixin):
    '''
    事件记录表
    '''
    __tablename__ = "audit_event"
    user_name = Column(String(32),index=True, nullable=False, comment='用户名称')
    event_name = Column(String(32),index=True, nullable=False, comment='事件名称')
    event_type = Column(String(24), nullable=False, comment='事件类型')
    is_notice = Column(Boolean, default=text('true'), comment="是否通知")
    create_time = Column(DateTime, default=datetime.datetime.now, comment="创建时间")
    comment = Column(String(1024), nullable=True, comment='事件说明')

    __table_args__ = (
        {
            'mysql_engine': 'innodb',
            'mysql_charset': 'utf8mb4',
            'mysql_collate': 'utf8mb4_unicode_ci',
            'extend_existing': True,
            'mysql_row_format': 'dynamic'
        }
    )

    def __repr__(self):
        return f"<name={self.user_name}, event_name:{self.event_name}'>"

    def object_to_dict(self, db: Session):
        result = {
            key: getattr(self, key)
            for key in self.__mapper__.c.keys()
        }
        return result

